﻿// Fill out your copyright notice in the Description page of Project Settings.


#include "Combat_Game_CPP/Public/Character/BaseCharacter.h"

#include "Actor/BaseWeapon.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "Helper/DebugHelper.h"


// Sets default values
ABaseCharacter::ABaseCharacter()
{
	// Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
}

// Called when the game starts or when spawned
void ABaseCharacter::BeginPlay()
{
	Super::BeginPlay();
}

// Called every frame
void ABaseCharacter::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
}

// Called to bind functionality to input
void ABaseCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);
}

void ABaseCharacter::PlayAttackMontage()
{
}

void ABaseCharacter::AttackDamage()
{
}


void ABaseCharacter::TakeHitDamage(const float DamageValue)
{
	Health = Health - DamageValue;
	if (Health <= 0)
	{
		Health = 0.0f;
		DeathResponse();
	}
	else
	{
		HurtResponse();
	}
}

void ABaseCharacter::HurtResponse()
{
	// 执行受伤动画
	PlayMontage(HurtAnimation);
}

void ABaseCharacter::DeathResponse()
{
	IsDeath_Anim = true;
	// 死亡清理逻辑
	DeathClear();
}

void ABaseCharacter::DeathClear()
{
}

void ABaseCharacter::SetMovementMode(EMovementMode MovementMode)
{
	GetCharacterMovement()->SetMovementMode(MovementMode);
}

void ABaseCharacter::ChangeWeaponAnimSocketName(ABaseWeapon* Weapon, FName AnimSocketName)
{
	if (Weapon)
	{
		// 放置在骨骼socket上
		FAttachmentTransformRules attachRules(EAttachmentRule::SnapToTarget, false);
		Weapon->AttachToComponent(GetMesh(), attachRules, AnimSocketName);
	}
}

void ABaseCharacter::PlayMontage(UAnimMontage* AnimMontage)
{
	UAnimInstance* AnimInstance = GetMesh()->GetAnimInstance();
	if (AnimInstance)
	{
		AnimInstance->Montage_Play(AnimMontage);
	}
}

float ABaseCharacter::GetHealthProgress()
{
	//Debug::printLog(FString::SanitizeFloat(Health / MaxHealth));
	return Health / MaxHealth;
}
